package com.tarena.lbs.basic.web.repoistory.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.mapper.AdminMapper;
import com.tarena.lbs.basic.web.repoistory.AdminRepository;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.dos.AdminDO;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class AdminRepositoryImpl implements AdminRepository {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminBO getUserByPhone(String phone) {
        //select all column from lbs_admin where account_phone=#{phone}
        //调用持久层接口 通过持久层接口费昂发的命名,可以了解sql语句
        log.debug("P-1持久层查询参数:{}",phone);
        AdminDO adminDO=adminMapper.selectAdminByPhone(phone);
        AdminBO adminBO=new AdminBO();
        //转化成bo返回
        if (adminDO!=null){
            BeanUtils.copyProperties(adminDO,adminBO);
        }
        return adminBO;
    }

    @Override
    public AdminBO getAdminById(Integer userId) {
        //1. 利用id查询 adminDO
        log.debug("P2-查询数据库:{}",userId);
        AdminDO adminDO=adminMapper.selectAdminById(userId);
        //2. 转化返回 考虑空
        AdminBO adminBO=null;
        if (adminDO!=null){
            adminBO=new AdminBO();
            BeanUtils.copyProperties(adminDO,adminBO);
        }
        return adminBO;
    }

    @Override
    public BasePageList<AdminBO> adminList(AdminQuery query) {
        //1.开启pageHelper 才能查询 total
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        //2.保证线程安全 startPage下一步必须是sql查询搜 pageHelper线程安全问题博文
        log.debug("P2-持久层查询:{}",query);
        List<AdminDO> adminDOS=adminMapper.selectAdminByPage(query);
        //3.数据转化封装成 bo list.stream.map|for
        PageInfo<AdminDO> pageInfo=new PageInfo<>(adminDOS);
        //在total不为空的时候 封装一个basePageList
        long total = pageInfo.getTotal();
        BasePageList<AdminBO> basePageList=null;
        if (total>0){
            List<AdminDO> dos = pageInfo.getList();
            List<AdminBO> bos = dos.stream().map(adminDO->{
                AdminBO bo=new AdminBO();
                BeanUtils.copyProperties(adminDO,bo);
                return bo;
            }).collect(Collectors.toList());
            basePageList=new BasePageList<>(bos,total);
        }else{
            basePageList=new BasePageList<>(null,0l);
        }
        return basePageList;
    }

    @Override
    public long countAdminByPhone(String accountPhone) {
        return adminMapper.selectCountByPhone(accountPhone);
    }

    @Override
    public void save(AdminBO adminBO) {
        AdminDO adminDO=new AdminDO();
        BeanUtils.copyProperties(adminBO,adminDO);
        adminMapper.insert(adminDO);
    }
}
